package com.zekdot.password.service.impl;

import com.zekdot.password.config.ConfigInfo;
import com.zekdot.password.dao.PasswordDataDao;
import com.zekdot.password.dto.Envelope;
import com.zekdot.password.entity.PasswordDataEntity;
import com.zekdot.password.entity.UserEntity;
import com.zekdot.password.service.PasswordDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * Create by zekdot on 19-8-8.
 */
@Service
public class PasswordDataServiceImpl implements PasswordDataService {
    @Resource
    private PasswordDataDao passwordDao;
    @Resource
    private ConfigInfo configInfo;

    @Override
    public Envelope insertPassword(PasswordDataEntity passwordEntity, UserEntity userEntity) {
        // 设置状态
        passwordEntity.setStatus("0");
        // 设置时间
        passwordEntity.setTime(new Timestamp(System.currentTimeMillis()));
        // 设置用户id
        passwordEntity.setUserId(userEntity.getUsername());
        // 存储密码
        passwordDao.insert(passwordEntity);
        return new Envelope(passwordEntity.getId());
    }

    @Override
    public Envelope getPasswordByPage(int lastId, int pageSize, UserEntity userEntity) {
        List<PasswordDataEntity> list;
        // 如果lastId为0
        if(lastId == 0){
            // 返回第一页的数据
            list = passwordDao.selectFirst(userEntity.getUsername(),pageSize);
        }else{
            // 返回更多数据
            list = passwordDao.selectMore(userEntity.getUsername(),lastId,pageSize);
        }
        return new Envelope(list);
    }

    @Override
    public Envelope deletePassword(int id, UserEntity userEntity) {
        // 非多用户情况下不允许删除密码，否则可能造成重要密码被恶意删除
        if (!configInfo.multiuser) {
            return new Envelope(-1, "非多用户情况下密码不可删除！", null);
        }
        PasswordDataEntity passwordEntity = passwordDao.get(id);
        if(passwordEntity == null){
            return new Envelope(1,"该密码不存在",null);
        }
//        System.out.println(id);
//        System.out.println(userEntity.getUsername());
//        System.out.println(passwordEntity.getUserId());
//        System.out.println(passwordEntity.getKeyword());
//        System.out.println(passwordEntity.getStatus());
        if(!passwordEntity.getUserId().equals(userEntity.getUsername())){
            return new Envelope(1,"您不能删除他人的密码",null);
        }
        // 实际删除操作
        passwordDao.delete(id);
        return Envelope.success;
    }

    @Override
    public Envelope searchPassword(String keyword, int lastId, int pageSize, UserEntity userEntity) {
        // 进行实际查询操作
        List<PasswordDataEntity> list = passwordDao.search(userEntity.getUsername(),"%"+keyword+"%",lastId,pageSize);
        // 隐藏关键数据
        return new Envelope(list);
    }

    @Override
    public Envelope getOnePassword(int id, UserEntity userEntity) {
        PasswordDataEntity passwordEntity = passwordDao.get(id);
        if(passwordEntity == null || !passwordEntity.getStatus().equals("0")){
            return new Envelope(1,"不存在id为" + id + "的密码",null);
        }
        if(!passwordEntity.getUserId().equals(userEntity.getUsername())){
            return new Envelope(1,"您不能查看他人的密码",null);
        }
        return new Envelope(passwordEntity);
    }

}
